using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Gencode.PrsUtil.Controls;

namespace Personalplaner
{
	public partial class PrintGridPrep : Form
	{
		DataGridView mainGrid; 
		DataGridView timeGrid;
		Gencode.PrsCommon.Data.Plan.Grid gridData;
		List<int> abteilungRows;
		List<PrinteableGrid> printeableGrids;

		int pageSize;
		int currentGridIndex = 0;
		bool closeAfterCreate = true;

		public PrintGridPrep(DataGridView mainGrid, DataGridView timeGrid, int pageSize, Gencode.PrsCommon.Data.Plan.Grid gridData)
		{
			InitializeComponent();

			this.mainGrid = mainGrid;
			this.timeGrid = timeGrid;
			this.pageSize = pageSize;
			this.gridData = gridData;

			abteilungRows = new List<int>();
			printeableGrids = new List<PrinteableGrid>();

			abteilungRows.Add(mainGrid.Rows.Count);
		}

		private void PrepareGridDerfaults()
		{
			//Set new Fontsize
			foreach (PrinteableGrid grid in printeableGrids)
			{
				grid.Grid.Font = new Font("Microsoft Sans Serif", 7);
			}
		}

		private void PrintGridPrep_Load(object sender, EventArgs e)
		{
			CreatePrinteableGrid();

			if (closeAfterCreate)
				this.Close();
		}

		private void CreatePrinteableGrid()
		{
			//Set amount of grids
			//if grid is bigger than 1 page then draw each abteilung in a separate grid
			int gridSize = 0;
			if (mainGrid.Rows.Count >= 1)
				gridSize = (mainGrid.Rows.Count * mainGrid.Rows[0].Height) + 124;
			
			if (gridSize > pageSize)
			{
				abteilungRows = new List<int>();
				int i = 0;
				int curAbt = gridData[i].AbteilungID;
				int prevAbt = curAbt;

				printeableGrids.Add(new PrinteableGrid());
				while (i + gridData.PreColumnCount <= mainGrid.Rows.Count)
				{
					prevAbt = curAbt;
					curAbt = gridData[i].AbteilungID;

					if (prevAbt != curAbt)
					{
						abteilungRows.Add(i + gridData.PreColumnCount - 1);
						printeableGrids.Add(new PrinteableGrid());
					}
					i++;
				}
			}
			else
				printeableGrids.Add(new PrinteableGrid());

			PrepareGridDerfaults();
			DrawGrids();


			int startIndex = 0;
			int endIndex = 1;
			if (currentGridIndex < abteilungRows.Count)
				endIndex = abteilungRows[currentGridIndex];
			else
				endIndex = abteilungRows.Count;
			while (currentGridIndex <= abteilungRows.Count && startIndex != endIndex)
			{
				DataTable dt = new DataTable("1");

				//Create Columns of new DataTable
				for (int i = 0; i < mainGrid.Columns.Count; i++)
				{
					dt.Columns.Add(mainGrid.Columns[i].HeaderText);
				}

				//Create Rows of new DataTable
				for (int i = startIndex; i < endIndex; i++)
				{
					DataRow dr = dt.NewRow();
					dr[0] = "";
					dt.Rows.Add(dr);
				}

				printeableGrids[currentGridIndex].DataSource = dt;

				//add Saldos
				if (PlanOption.PrintSoll == 1)
				{
					dt.Columns.Add("Soll");
					for (int i = startIndex; i < endIndex; i++)
					{
						double rnd;
						string value = "";

						if (timeGrid[1, i].Value != null)
						{
							if (timeGrid[1, i].Value.ToString() != String.Empty)
							{
								rnd = (double)(int)(100 * (double.Parse(timeGrid[1, i].Value.ToString())));
								value = "" + (rnd / 100);
							}
						}
						printeableGrids[currentGridIndex].Grid[printeableGrids[currentGridIndex].Grid.Columns.Count - 1, i - startIndex].Value = value;
					}
					printeableGrids[currentGridIndex].Grid.Columns[printeableGrids[currentGridIndex].Grid.Columns.Count - 1].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
				}

				if (PlanOption.PrintHaben == 1)
				{
					dt.Columns.Add("Haben");
					for (int i = startIndex; i < endIndex; i++)
					{
						double rnd;
						string value = "";

						if (timeGrid[2, i].Value != null)
						{
							if (timeGrid[1, i].Value.ToString() != String.Empty)
							{
								rnd = (double)(int)(100 * (double.Parse(timeGrid[2, i].Value.ToString())));
								value = "" + (rnd / 100);
							}
						}

						printeableGrids[currentGridIndex].Grid[printeableGrids[currentGridIndex].Grid.Columns.Count - 1, i - startIndex].Value = value;
					}
					printeableGrids[currentGridIndex].Grid.Columns[printeableGrids[currentGridIndex].Grid.Columns.Count - 1].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
				}

				if (PlanOption.PrintSaldoDiff == 1)
				{
					dt.Columns.Add("Diff");
					for (int i = startIndex; i < endIndex; i++)
					{
						double rnd;
						string value = "";

						if (timeGrid[3, i].Value != null)
						{
							if (timeGrid[2, i].Value.ToString() != String.Empty)
							{
								try
								{
									rnd = (double)(int)(100 * double.Parse(timeGrid[4, i].Value.ToString()));
									value = "" + (rnd / 100);
								}
								catch
								{
									value = timeGrid[4, i].Value.ToString();
								}
							}
						}

						printeableGrids[currentGridIndex].Grid[printeableGrids[currentGridIndex].Grid.Columns.Count - 1, i - startIndex].Value = value;
					}
					printeableGrids[currentGridIndex].Grid.Columns[printeableGrids[currentGridIndex].Grid.Columns.Count - 1].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
				}

				//set height of cells
				for (int r = startIndex; r < endIndex; r++)
				{
					printeableGrids[currentGridIndex].Grid.Rows[r - startIndex].Height = mainGrid.Rows[r].Height;
				}

				//Copy all Data from mainGrid
				for (int c = 0; c < mainGrid.Columns.Count; c++)
				{
					for (int r = startIndex; r < endIndex; r++)
					{
						printeableGrids[currentGridIndex].Grid[c, r - startIndex].Value = mainGrid[c, r].Value;
						printeableGrids[currentGridIndex].Grid[c, r - startIndex].Style.BackColor = mainGrid[c, r].Style.BackColor;
						printeableGrids[currentGridIndex].Grid[c, r - startIndex].Style.ForeColor = mainGrid[c, r].Style.ForeColor;
					}
					printeableGrids[currentGridIndex].Grid.Columns[c].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
				}

				foreach (DataGridViewColumn column in printeableGrids[currentGridIndex].Grid.Columns)
				{
					column.SortMode = DataGridViewColumnSortMode.NotSortable;
				}

				if (currentGridIndex < abteilungRows.Count)
					startIndex = abteilungRows[currentGridIndex];
				
				currentGridIndex++;

				if (currentGridIndex < abteilungRows.Count)
					endIndex = abteilungRows[currentGridIndex];
				else
					endIndex = mainGrid.Rows.Count;
			}
		}

		private void DrawGrids()
		{
			foreach (PrinteableGrid grid in printeableGrids)
			{
				TabPage page = new TabPage("");
				grid.Dock = DockStyle.Fill;
				page.Controls.Add(grid);
				gridPage.TabPages.Add(page);
				gridPage.SelectedIndex = gridPage.TabPages.Count - 1;
			}
		}

		public DataGridView Grid
		{
			get
			{
				return printeableGrids[currentGridIndex].Grid;
			}
		}

		public List<DataGridView> Grids
		{
			get
			{
				List<DataGridView> tmp = new List<DataGridView>();
				for (int i = 0; i < printeableGrids.Count; i++)
				{
					tmp.Add(printeableGrids[i].Grid);
				}
				return tmp;
			}
		}

		/// <summary>
		/// Close the Dialog automaticaly after the grids are created.
		/// </summary>
		public bool CloseAfterCreate
		{
			get
			{
				return closeAfterCreate;
			}
			set
			{
				closeAfterCreate = value;
			}
		}
		
	}
}